﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using CommunitySystem.GuaZhiDuanLianService;

namespace CommunitySystem
{
    public partial class GuaZhiDuanLianJiHuaTitleWindow : ChildWindow
    {
        private GuaZhiDuanLianServiceClient Client =
            new GuaZhiDuanLianServiceClient();
        private int Year;
        public int ID;
        public bool IsNew = false;
        private GuaZhiDuanLianJiHuaModel model = new GuaZhiDuanLianJiHuaModel();
        public GuaZhiDuanLianJiHuaTitleWindow(int year, int iD, bool isnew)
        {
            InitializeComponent();
            Year = year;
            ID = iD;
            IsNew = isnew;
            Client.InsertJuZhuanAnZhiJiHuaTitleCompleted += new EventHandler<InsertJuZhuanAnZhiJiHuaTitleCompletedEventArgs>(Client_InsertJuZhuanAnZhiJiHuaTitleCompleted);
            if (IsNew)
            {
                this.Title = "新增安置计划";
                model = new GuaZhiDuanLianJiHuaModel();
                this.DataContext = model;
            }
            else
            {
                this.Title = "编辑安置计划信息";
                Client.GetGuaZhiDuanLianJiHuaModelCompleted += new EventHandler<GetGuaZhiDuanLianJiHuaModelCompletedEventArgs>(Client_GetGuaZhiDuanLianJiHuaModelCompleted);
                Client.GetGuaZhiDuanLianJiHuaModelAsync(ID, "",0);
            }
        }

        void Client_GetGuaZhiDuanLianJiHuaModelCompleted(object sender, GetGuaZhiDuanLianJiHuaModelCompletedEventArgs e)
        {
            if (e.Result != null)
                model = e.Result[0];
            this.DataContext = model;
        }

        private void CancelButton_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = false;
        }

        private void OKButton_Click(object sender, RoutedEventArgs e)
        {
            model.Year = Year;
            Client.InsertJuZhuanAnZhiJiHuaTitleAsync(model);
        }

        void Client_InsertJuZhuanAnZhiJiHuaTitleCompleted(object sender, InsertJuZhuanAnZhiJiHuaTitleCompletedEventArgs e)
        {
            if (model.ID != 0)
                SilverlightMessageBoxLibrary.Message.InfoMessage("编辑成功！");
            else
                SilverlightMessageBoxLibrary.Message.InfoMessage("新增成功！");
            this.DialogResult = true;
        }
    }
}
